/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.staf.NetworkReportGenerator.V2;

import java.net.URL;
import java.net.HttpURLConnection;
import java.io.InputStream;
import java.io.LineNumberReader;
import java.io.InputStreamReader;
import sun.misc.BASE64Encoder;

import java.io.IOException;

/**
 *
 * @author aezhov
 */
public class HtmlUtil {

    public static String readURL(String urlStr) {
        System.out.println("HtmlUtil.readURL: " + urlStr);

        try {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            InputStream is = conn.getInputStream();
            LineNumberReader reader = new LineNumberReader(new InputStreamReader(is));

            String line = null;
            String doc = "";
            while ((line = reader.readLine()) != null) {
                doc += line;
            }

            String[] parts = doc.split("\\s+");
            doc = "";
            for (int i = 0; i < parts.length; i++) {
                doc += parts[i].toLowerCase();
            }

            return doc;

        } catch (java.net.MalformedURLException exc) {
            System.out.println("HtmlUtil: URL exception " + exc);
        } catch (java.io.IOException exc) {
            System.out.println("HtmlUtil: IO exception " + exc);
            //exc.printStackTrace();
        } catch (Exception exc) {
            System.out.println("HtmlUtil: Exception during html processing" + exc);
        }

        return null;
    }

    public static String readURL(String urlStr, String login, 
                                 String password) throws IOException
    {
        String doc = "";

        BASE64Encoder encoder = new BASE64Encoder();
        String secStrToEncode = login + ":" + password;
        String encodedSec = encoder.encode(secStrToEncode.getBytes());

        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Authorization", "Basic "
                + encodedSec);

        InputStream is = conn.getInputStream();
        LineNumberReader reader = new LineNumberReader(new InputStreamReader(is));

        String line = null;
        while ((line = reader.readLine()) != null) {
            doc += line;
        }

        String[] parts = doc.split("\\s+");
        doc = "";
        for (int i = 0; i < parts.length; i++) {
            doc += parts[i].toLowerCase();
        }

        return doc;
    }
}
